package cn.lxh.org.controller;

import cn.lxh.basic.exception.BusinessException;
import cn.lxh.basic.utils.ExcelUtil;
import cn.lxh.basic.utils.PageList;
import cn.lxh.org.basic.utils.AjaxResult;
import cn.lxh.org.domain.ShopAuditLog;
import cn.lxh.org.service.IShopService;
import cn.lxh.org.domain.Shop;
import cn.lxh.org.query.ShopQuery;
import cn.lxh.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;

    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if(shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.queryById(id);
    }

    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){
        return shopService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList json(@RequestBody ShopQuery query)
    {
        return shopService.queryList(query);
    }

    /*第一步店铺入住的判断*/


    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("运行时候的异常"+e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统的大的异常"+e.getMessage());
        }
    }

    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog){
        try {

            shopService.pass(shopAuditLog);
            return AjaxResult.me();

        } catch (Exception e) {
           e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMessage("通过操作失败");
        }
    }


    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.reject(shopAuditLog);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false).setMessage("驳回操作操作失败");
        }
    }
    /**
     * 表格导出
     * @param response
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        try {
            List<Shop> shops = shopService.queryAll();
            ExcelUtil.exportExcel(shops,null,"店铺信息",Shop.class,"店铺信息.xls",response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
/*表格的导入*/
    @PostMapping("/importExcel")
    public  void importExcel(@RequestPart MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        //批量新增
        shopService.batchAdd(shops);

    }
/*echarts的显示图片*/
    @GetMapping("/echarts")
    public Map<String,Object> echarts(){
        List<ShopVo>  vos = shopService.queryEcharts();

        List<Integer> y = vos.stream().map(ShopVo::getNum).collect(Collectors.toList());
        List<String> x = new ArrayList<>();
        for (ShopVo vo : vos) {
            String state = vo.getState();
            x.add(state);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("x",x);
        map.put("y",y);
        return map;
    }
}
